package com.teradata.dashboard.constant;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import com.teradata.dashboard.DashboardApplication;
import com.teradata.dashboard.utils.FileHelper;

public class DashboardConstant {

	private static Map paraMap = new HashMap();

	public static final String AUTH_KEY_COOKIE_NAME = "TPIFCOOKIE";

	public static final String AUTH_KEY_COOKIE_PASS = "TPIFCOOKIE-PASS";

	public static final String AUTH_KEY_COOKIE_USERID = "TPIFCOOKIE-USERID";
	
	public final static String AUTO_LOAD = "AUTO_LOAD";
	public final static String FONT_SIZE = "FONT_SIZE";
	public final static String FONT_SIZE_FILE = "fontSize.bin";

	// 数据项
	public static final String KPI_DATAITEM_VALUE = "KPI_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_ASSESS_VALUE = "ASSESS_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_ASSESS_COMPLETE_VALUE = "ASSESS_COMPLETE_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_ASSESS_COMPLETE_RATE = "ASSESS_COMPLETE_RATE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_FREQ_VALUE = "LAST_FREQ_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_FREQ_CMP_VALUE = "LAST_FREQ_CMP_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_FREQ_CMP_RATE = "LAST_FREQ_CMP_RATE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_YEAR_VALUE = "LAST_YEAR_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_YEAR_CMP_VALUE = "LAST_YEAR_CMP_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_YEAR_CMP_RATE = "LAST_YEAR_CMP_RATE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_YEAR_END_VALUE = "LAST_YEAR_END_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_YEAR_END_CMP_VALUE = "LAST_YEAR_END_CMP_VALUE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_LAST_YEAR_END_CMP_RATE = "LAST_YEAR_END_CMP_RATE"; // 基础类指标策略，所有的指标都归结为这三类中
	public static final String KPI_DATAITEM_ORG_DUTY_RADIO_VALUE = "ORG_DUTY_RADIO_VALUE"; // 机构占比

	static {

		initPara();
	}

	public static void initPara() {
		Field[] fields = DashboardConstant.class.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if (Modifier.isFinal(fields[i].getModifiers())) {
				continue;
			}
			String paraName = fields[i].getName();
			try {
				if (fields[i].getType().equals(String.class)) {
					fields[i].set(
							DashboardConstant.class,
							getPara(paraName, fields[i].get(paraName)
									.toString()));
				} else if (fields[i].getType().equals(int.class)) {
					fields[i].setInt(
							DashboardConstant.class,
							Integer.parseInt(getPara(paraName,
									fields[i].get(paraName).toString())));
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static String getPara(String paraName, String defaultValue) {
		if ("".equals(paraName) || paraName == null) {
			return defaultValue;
		}
		paraName = paraName.trim();
		if (paraMap.get(paraName) != null) {
			return paraMap.get(paraName).toString();
		}
		// get parameter from province file
		Properties props = FileHelper.readProperties(DashboardApplication.app,
				"config/config.properties");
		if (props.containsKey(paraName)) {
			paraMap.put(paraName, props.getProperty(paraName, ""));
		} else {
			paraMap.put(paraName, defaultValue);
		}
		System.out.println("------------"+paraName+"------------------------");
		System.out.println("------------"+paraMap.get(paraName).toString()+"------------------------");
		return paraMap.get(paraName).toString();
	}

	public static void refreshPara() {
		paraMap = new HashMap();
		initPara();
	}

}
